<?php

/**
 * powerdev actions.
 *
 * @package    seth
 * @subpackage powerdev
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class powerdevActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
      $q= Doctrine_Core::getTable('powerdev')->createQuery()
              ->addOrderBy('type ASC')
              ->addOrderBy('plant ASC');
      $this->pager=new sfDoctrinePager('Powerdev',  sfConfig::get('app_max_page_rows'));
      $this->pager->setQuery($q);
      $this->pager->setPage($request->getParameter('page',1));
      $this->pager->init();
  }

  public function executeShow(sfWebRequest $request)
  {
    $this->powerdev = $this->getRoute()->getObject();
    $varDev=$this->powerdev->getVarLastUpdate();
    if (!is_null($varDev))
        $this->data=$varDev->getNotNullMeasures();
  }
  

  public function executeNew(sfWebRequest $request)
  {
    $this->formMany=new PowerdevFixturesForm();
    $this->form = new PowerdevForm();
    
 
    if ($request->hasParameter("plant")){
        $this->formMany->setDefault ("plant", $request->getParameter('plant'));
        $this->form->setDefault ("plant", $request->getParameter('plant'));
    }
    
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->form = new PowerdevForm();
    
    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->form = new PowerdevForm($this->getRoute()->getObject());
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->form = new PowerdevForm($this->getRoute()->getObject());

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();
    
    //delete powerdev related data
    $this->getRoute()->getObject()->delVar();        
    $this->getRoute()->getObject()->delete();
    
    $this->redirect('plant/show?id='. $this->getRoute()->getObject()->getPlant()->getId());
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $powerdev = $form->save();

      $this->redirect('powerdev/edit?id='.$powerdev->getId());
    }
  }
  
  public function executeGetFixtures(sfWebRequest $request)
  {
      $this->forward404Unless($request->isMethod('post'));
      //$this->setLayout(false);
      
      $this->formMany=new PowerdevFixturesForm();
      $this->formMany->bind($request->getParameter('fixtures'), $request->getFiles('fixtures'));
      if ($this->formMany->isValid()){
      
            $this->plant=Doctrine_Core::getTable("Plant")->find($this->formMany->getValue("plant"));
           
            if (is_null($this->formMany->getValue("file"))){
                    //preparing excel fixtures file and forward to download    
                    $filename=Doctrine_Core::getTable("Powerdev")->getExcelInsertFile($this->plant);
                    if (!$filename)
                         throw new Exception("Error generating Excel file. Please try again later..");
                    
                    $path=  sfConfig::get("app_temp_data_folder")."/".$filename;
                    
                    // Adding the file to the Response object
                    $this->getResponse()->clearHttpHeaders();
                    $this->getResponse()->setHttpHeader('Pragma: public', true);
                    $this->getResponse()->setHttpHeader('Content-Disposition', 'attachment; filename='.$filename);
                    $this->getResponse()->setContentType('application/vnd.ms-excel');
                    $this->getResponse()->sendHttpHeaders();
                    readfile($path);
                    
                    return sfView::HEADER_ONLY; 
            }else{
                   //processing uploaded fixtures file
                    $file=$this->formMany->getValue("file");
                    $ext=$file->getOriginalExtension();
                    $filename=str_replace($ext,"", $file->getOriginalName())."_U";
                    
                    if (strpos(strtoupper($filename), strtoupper(str_replace(" ","_",$this->plant->getName())))===FALSE)
                        throw new Exception("Error: imported filed ".$filename." while selected plant ".$this->plant->getName());
                            
                    
                    $path= sfConfig::get("app_temp_data_folder")."/".$filename.$ext;
                    $file->save($path);
 
                    $result=Doctrine_Core::getTable("Powerdev")->importExcelInsertFile($path, $this->plant);
                    
                    if ($result===FALSE){
                         throw new Exception("Error inserting data from uploaded Excel file. Please try again");
                    }elseif(isset($result["errors"])){
                        $this->errors=$result["errors"];
                        $this->setTemplate("new");
                    }else{
                        $this->redirect("plant/show?id=".$this->plant->getId());  
                    }
                        
                          
            }           
    }else{
            $this->setTemplate("new");
    }          
  }

}
